import axios from "axios";
import { notification } from "antd";

// 数据返回的接口

const RequestEnums = {
  OVERDUE: 600, // 登录失效
  FAIL: 999, // 请求失败
  SUCCESS: 200, // 请求成功
};

class RequestHttp {
  // 定义成员变量并指定类型
  service;
  constructor(config) {
    // 实例化axios
    this.service = axios.create(config);
    this.service.interceptors.request.use(
      (config) => {
        return config;
      },
      (error) => {
        // 请求报错
        Promise.reject(error);
      }
    );

    /**
     * 响应拦截器
     * 服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息
     */
    this.service.interceptors.response.use(
      (response) => {
        const { data, config } = response; // 解构
        if (response.status === RequestEnums.OVERDUE) {
          return Promise.reject(data);
        }
        // 全局错误信息拦截（防止下载文件得时候返回数据流，没有code，直接报错）
        if (response.status && response.status !== RequestEnums.SUCCESS) {
          return Promise.reject(data);
        }
        return data;
      },
      (error) => {
        const { response } = error;
        console.log(error);
        if (response) {
          notification.error({
            message: response.statusText,
            description: `status:${response.status} statusText:${response.statusText}`,
          });
          this.handleCode(response.status, response.statusText);
        } else {
          switch (error.code) {
            case "ECONNABORTED":
              notification.error({
                message: "服务响应超时~~~",
                description: error.message,
              });
              break;
              case "ERR_NETWORK":
                notification.error({
                  message: "网络错误~~~",
                  description: error.message,
                });
                break
          }
        }
        if (!window.navigator.onLine) {
          // router.replace({
          //   path: "/404",
          // });
          return;
        }
      }
    );
  }
  handleCode(code, statusText) {
    switch (code) {
      case 401:
        break;
      case 500:
        break;
      default:
        break;
    }
  }
  needTokenFun(boo) {
    if (typeof boo == "boolean" && boo) {
      // const token = Cookies.get('token');
      if (token) {
        return { authorization: "Bearer " + token };
      } else {
        // Notification.info("您还未登录呦~~~~")
        return {};
      }
    } else if (Object.prototype.toString.call(boo) == "[object Object]") {
      return boo;
    } else {
      return {};
    }
  }

  get(url, params, boo) {
    const header = this.needTokenFun(boo);
    return this.service.get(url, {
      params,
      headers: {
        ...header,
      },
    });
  }
  post(url, params, boo) {
    const header = this.needTokenFun(boo);
    return this.service.post(url, params, {
      headers: {
        ...header,
      },
    });
  }
  put(url, params, boo) {
    const header = this.needTokenFun(boo);
    return this.service.put(url, params, {
      headers: {
        ...header,
      },
    });
  }
  delete(url, params, boo) {
    const header = this.needTokenFun(boo);
    return this.service.delete(url, {
      params,
      headers: {
        ...header,
      },
    });
  }
}

// 导出一个实例对象
// export default new RequestHttp(config);
export default RequestHttp;
